package com.onpositive.notes.label.providers;

import java.util.Locale;

import com.onpositive.notes.impl.AbstractEntityWithClass;
import com.onpositive.notes.model.IArgumentLabelProvider;
import com.onpositive.notes.model.ITemplateArgument;

public class BasicLabelProvider<T> extends AbstractEntityWithClass<T> implements IArgumentLabelProvider<T> {
	
	public BasicLabelProvider() {
		super();
	}

	private static final long serialVersionUID = 7182365685057473171L;
	
	public static BasicLabelProvider<String> instance = new BasicLabelProvider<String>(String.class) ;	

	public static BasicLabelProvider<String> getInstance() {
		return instance;
	}
	
	public BasicLabelProvider(Class<T> clazz) {
		super(clazz);
	}

	@Override
	public String computeLabel(ITemplateArgument<?> arg) {
		
		Class<?> argClass = arg.getSubjectClass();
		if( argClass!=null && argClass.equals( getSubjectClass() )  ){
			
			@SuppressWarnings("unchecked")
			ITemplateArgument<T> resolved = (ITemplateArgument<T>) arg ;
			return doComputeLabel( resolved ) ;			
		}
			
		Object argumentValue = arg.getArgumentValue();
		if( argumentValue == null )
			return "NULL" ;
		
		return argumentValue.toString() ;
	}

	protected String doComputeLabel(ITemplateArgument<T> arg) {
		return arg.getArgumentValue().toString() ;
	}

	@Override
	public String computeLabel(ITemplateArgument<?> arg, Locale locale) {
		return computeLabel(arg) ;
	}

}
